package com.burgeon.backend.EnterpriseWeChat.rest.Analysis;

import com.burgeon.backend.EnterpriseWeChat.entity.StoreCRM;
import com.burgeon.backend.EnterpriseWeChat.entity.StoreKpi;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 购物车推荐
 */
public class getStoreCRM extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public getStoreCRM(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public HashMap<String, ArrayList<String>>  getData(String empId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(empId), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<StoreCRM> storeKpis= new ArrayList<>();

        ArrayList<String> TB = new ArrayList<String>();
        ArrayList<String> HB = new ArrayList<>();

        if(jsonArray.length()>0){
            for (int i = 0; i <24; i++) {
//                StoreCRM storeCRM = new StoreCRM();
                TB.add(jsonArray.getJSONArray(0).getString(i));
                HB.add(jsonArray.getJSONArray(0).getString(i+1));
                System.err.println(i);
                i=i+4;

            }
        }
        HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
        map.put("TB",TB);
        map.put("HB",HB);
        return map;
    }

    private MultiValueMap Data(String empId) {
        HashMap<String, java.io.Serializable> params = new HashMap<>();
        ArrayList<String> colums = new ArrayList<>();//查询字段

        //成交人数
        colums.add("'SALER_TB_COUNTNO'");
        colums.add("SALER_HB_COUNTNO");
        colums.add("TOT_NSALER_NO");
        colums.add("TOT_QSALER_NO");
        colums.add("TOT_QLSALER_NO");

        //连带率
        colums.add("'SALER_TB_LDL'");
        colums.add("SALER_HB_LDL");
        colums.add("TOT_LDL_NDAY");
        colums.add("TOT_LDL_QDAY");
        colums.add("TOT_LDL_QLDAY");

        //平均价
        colums.add("SALER_TB_JDJ");
        colums.add("SALER_HB_JDJ");
        colums.add("TOT_JDJ_NDAY");
        colums.add("TOT_JDJ_QDAY");
        colums.add("TOT_JDJ_QLDAY");
        //客单价
        colums.add("SALER_TB_KDJ");
        colums.add("SALER_HB_KDJ");
        colums.add("TOT_KDJ_NDAY");
        colums.add("TOT_KDJ_QDAY");
        colums.add("TOT_KDJ_QLDAY");

        //月销售
        colums.add("TOT_TBSALER_AMT");
        colums.add("TOT_HBSALER_AMT");
        colums.add("TOT_NSALER_PRICE");
        colums.add("TOT_QSALER_PRICE");
        colums.add("TOT_QLSALER_PRICE");

        params.put("columns", colums);
        params.put("table", "S_BULLETINBOARD2");//表名
        params.put("count", "true");

        HashMap<String, String> paramss = new HashMap<String, String>();

        paramss.put("column","'SALESREP_ID'");
        String a = "'"+empId+"'";
        paramss.put("condition",a);

//        HashMap orderby = new HashMap();
//
//        orderby.put("column", "BILLDATE");
//        orderby.put("asc", "true");
//        ArrayList list = new ArrayList();
//        list.add(orderby);
//
//        params.put("orderby", list);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getStoreCRM g = new getStoreCRM("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("5223"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
